//
//  YWErrorRecordFunctionViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 17/2/23.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWErrorRecordFunctionViewController.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "YWCommonTool.h"
#import "UIColor+CustomColors.h"
#import "ASProgressPopUpView.h"
#import "YWLogRecordTool.h"

@interface YWErrorRecordFunctionViewController ()
@property (nonatomic, strong) UILabel * tipLabel;
@property (nonatomic, strong) UILabel * statusLabel;
@property (nonatomic, strong) UIButton * uploadLogBtn;
@property (nonatomic, strong) ASProgressPopUpView* progressView;
@end

@implementation YWErrorRecordFunctionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"errorDiagnosis", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    [self setUpSubviews];
}

- (void)setUpSubviews{
    [self.view addSubview:self.tipLabel];
    [self.view addSubview:self.statusLabel];
    [self.view addSubview:self.uploadLogBtn];
    [self.view addSubview:self.progressView];
}

- (void)uploadLogRecordFile:(UIButton*)button{
    button.userInteractionEnabled = NO;
    [button setTitle:NSLocalizedString(@"uploading", nil) forState:UIControlStateNormal];
    BOOL existLog = [[YWLogRecordTool shareLogRecordManager] existLogRecordFile];
    if (existLog == NO) {
        NSString *tip = NSLocalizedString(@"noErrorLogs", nil);
        UIAlertController *alertController= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"tips", nil) message:tip preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self noExistLogWithButton:button];
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }else{
        self.statusLabel.hidden = NO;
        self.progressView.hidden = NO;
        [[YWLogRecordTool shareLogRecordManager] uploadLogRecordToAliyunOSSProgressBlock:^(float progress) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.progressView setProgress:progress animated:YES];
            });
        } completeBlock:^(BOOL success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (success) {
                    [self uploadSuccessWithButton:button];
                }else{
                    [self uploadFailedWithButton:button];
                }
            });
        }];
    }
}

- (void)noExistLogWithButton:(UIButton*)button{
    button.backgroundColor = [UIColor customTextGrayColor];
    [button setTitle:NSLocalizedString(@"uploadErrorLogs", nil) forState:UIControlStateNormal];
}

- (void)uploadSuccessWithButton:(UIButton*)button{
    button.userInteractionEnabled = NO;
    button.backgroundColor = [UIColor customTextGrayColor];
    self.statusLabel.text = NSLocalizedString(@"uploadSucceed", nil);
    [button setTitle:NSLocalizedString(@"uploadSucceed", nil) forState:UIControlStateNormal];
}

- (void)uploadFailedWithButton:(UIButton*)button{
    button.userInteractionEnabled = YES;
    [button setTitle:NSLocalizedString(@"reUpload", nil) forState:UIControlStateNormal];
    button.backgroundColor = [UIColor customBlueColor];
    [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
}

#pragma mark  --- 懒加载
- (UILabel *)tipLabel{
    if (!_tipLabel) {
        UILabel * tipLabel = [[UILabel alloc] init];
        NSInteger existLog = [[YWLogRecordTool shareLogRecordManager] existLogRecordFile];
        NSString* fileSize = [[YWLogRecordTool shareLogRecordManager] logFileSize];
        NSString *errorLogCount = NSLocalizedString(@"errorLogCount", nil);
        NSString *errorLogFileSize = NSLocalizedString(@"errorLogFileSize", nil);
        NSString *errorLogTip = NSLocalizedString(@"errorLogTip", nil);
        NSString * tipString = [NSString stringWithFormat:@"%@%d%@（文件大小：%@），%@",errorLogCount,(existLog?1:0),errorLogFileSize,fileSize,errorLogTip];
        tipLabel.text = tipString;
        tipLabel.font = kContentFont;
        tipLabel.numberOfLines = 0;
        CGFloat locationH = [YWCommonTool getHeightOfLabel:tipLabel.text andWidth:(SCREEN_WIDTH-2*kMarginW) andFont:kContentFont andMinHeight:4*kLabelH];
        [tipLabel setFrame:CGRectMake(kMarginW, kGapH, SCREEN_WIDTH-2*kMarginW, locationH)];
        _tipLabel = tipLabel;
    }
    return _tipLabel;
}

- (UILabel *)statusLabel{
    if (!_statusLabel) {
        UILabel * statusLabel = [[UILabel alloc] init];
        statusLabel.text = NSLocalizedString(@"uploading", nil);
        [statusLabel setFrame:CGRectMake(kMarginW, CGRectGetMaxY(self.tipLabel.frame)+2*kGapH, SCREEN_WIDTH-2*kMarginW, kLabelH)];
        statusLabel.font = kContentFont;
        statusLabel.hidden = YES;
        _statusLabel = statusLabel;
    }
    return _statusLabel;
}

- (ASProgressPopUpView*)progressView
{
    if (!_progressView) {
        ASProgressPopUpView * progressView = [[ASProgressPopUpView alloc] initWithFrame:CGRectMake(kMarginW,CGRectGetMaxY(self.statusLabel.frame)+4*kGapH, SCREEN_WIDTH-kMarginW*2, 30)];
        progressView.popUpViewAnimatedColors = @[ [UIColor customYellowColor] ];
        progressView.tintColor = [UIColor customYellowColor];
        progressView.popUpViewCornerRadius = 12.0;
        progressView.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:16];
        [progressView showPopUpViewAnimated:YES];
        progressView.hidden = YES;
        _progressView = progressView;
    }
    return _progressView;
}

-(UIButton *)uploadLogBtn{
    if (!_uploadLogBtn) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setFrame:CGRectMake(kMarginW,CGRectGetMaxY(self.progressView.frame)+2*kGapH, SCREEN_WIDTH-2*kMarginW, kButtonH)];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4.0;
        
        button.titleLabel.font = kContentFont;
        [button setTitle:NSLocalizedString(@"uploadErrorLogs", nil) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor customBlueColor]];
        [button addTarget:self action:@selector(uploadLogRecordFile:) forControlEvents:UIControlEventTouchUpInside];
        _uploadLogBtn = button;
    }
    return _uploadLogBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
